﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WutiWTO.Common;
using WuxiWTO.Mapping;
using System.Data.Linq;

namespace WuxiWTO.Web.backend
{
    public partial class SiteSetting : BasePage
    {
        WTODataContext contex = new WTODataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                GetSiteInfo();
            }
        }

        private void GetSiteInfo()
        {
            site_setting setting = contex.site_settings.SingleOrDefault<site_setting>(p => p.ID == 1);
            if (setting != null)
            {
                this.txtAddress.Text = setting.site_address;
                this.txtCopyR.Text = setting.site_copyright;
                this.txtDesc.Text = setting.site_description;
                this.txtRecNo.Text = setting.site_ipc;
                this.txtKey.Text = setting.site_keywords;
                this.txtSietName.Text = setting.site_name;
                this.txtTel.Text = setting.site_telephone;
                if (setting.allow_register.HasValue)
                {
                    if (setting.allow_register.Value == '0')
                    {
                        this.chkRegister.Checked = false;
                    }
                    else
                    {
                        this.chkRegister.Checked = true;
                    }
                }
                else
                {
                    this.chkRegister.Checked = false;
                }

                if (setting.allow_reg_validate.HasValue)
                {
                    if (setting.allow_reg_validate.Value == '0')
                    {
                        this.chkRegValidate.Checked = false;
                    }
                    else
                    {
                        this.chkRegValidate.Checked = true;
                    }
                }
                else
                {
                    this.chkRegValidate.Checked = false;
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool createMode = false;
            try
            {
                site_setting setting = contex.site_settings.SingleOrDefault<site_setting>(p => p.ID == 1);
                if (setting == null)
                {
                    setting = new site_setting();
                    createMode = true;
                }
                setting.site_address = this.txtAddress.Text.Trim();
                setting.site_copyright = this.txtCopyR.Text.Trim();
                setting.site_description = this.txtDesc.Text.Trim();
                setting.site_ipc = this.txtRecNo.Text.Trim();
                setting.site_keywords = this.txtKey.Text.Trim();
                setting.site_name = this.txtSietName.Text.Trim();
                setting.site_telephone = this.txtTel.Text.Trim();
                setting.allow_register = this.chkRegister.Checked ? '1' : '0';
                setting.allow_reg_validate = this.chkRegValidate.Checked ? '1' : '0';
                if (createMode)
                    contex.site_settings.InsertOnSubmit(setting);
                contex.SubmitChanges();
                ClientScript.RegisterStartupScript(this.GetType(),"suc","alert('网站信息更新成功');",true);
            }
            catch(Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "suc", "alert(\"Err:" + ex.Message.Replace("\"", "").Replace("\r\n", "") + "\");", true);
            }
        }
    }
}